var app = getApp();
var time = require('../../../utils/util.js');
Page({
  data: {
    // getBooksByCondition: app.globalData.bookUrl + '/book/getBooks.do',
    // borrow: app.globalData.bookUrl + '/user/useBook.do',
    // register: app.globalData.bookUrl + '/user/userRegister.do', //注册
    // userInfo: app.globalData.bookUrl + '/user/userInfo.do', //判断是否已注册
    // getBookType: app.globalData.bookUrl + '/book/bookType.do',
    // notReturned: app.globalData.bookUrl + '/user/getBorrowing.do', //获取当前用户未归还图书id
    // backBook: app.globalData.bookUrl + '/user/backBook.do',
    // hiddenmodalput: true,
    // readerNameing: "",
    // bookidL: '',
    // scanflag: 0,
    getEvaluateURL: app.globalData.bookUrl + '/evaluate/selectEvaluate.do',//查询评价
    praiseURL: app.globalData.bookUrl + '/evaluate/praise.do',//点赞记录
    evaluateList: [],
    totalElements: 0,
    currentPage: 1,
    num: 4,//后端给的分数，显示的星星
    illume: '',//点亮的星星数
    illume_no: '',//没有点亮的星星数
    color:'',
    _num: 0,
  },

  onLoad: function (options) {
    //星星显示
    var that = this;
  
    that.setData({
      bookName: options.bookName
    })
    that.getEvaluate(that.data.currentPage);
  },
  onPullDownRefresh: function () {
    var that = this;
    // 显示顶部刷新图标
    that.setData({
      evaluateList: []
    });
    wx.showNavigationBarLoading({
      title: '正在刷新',
    })
    // 页数+1
    that.getEvaluate(1);
   
  },

  onReachBottom: function () {
    var that = this;
    var evaluateListResult = that.data.evaluateList;
    if (evaluateListResult.length == that.data.totalElements) { //最大数值
        wx.showToast({
          title: "已到达底部",
          icon: "success",
          duration: 300
        });
        return false;
      } else {
        // 显示加载图标
        // wx.showLoading({
        //   title: '玩命加载中',
        // })
        // 页数+1
      that.getEvaluate(that.data.currentPage);
      }
  },
  getEvaluate: function (currentPage) {
    var that = this;
    var openId = wx.getStorageSync('openid');
    wx.request({
      url: that.data.getEvaluateURL,
      data: {
        bookName: that.data.bookName,
        openid: openId,
        praise: "1",
        currentPage: currentPage
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log("fewafaewfawf"+res.data.data);
        that.showEvaluate(res.data.data, currentPage); //向前台传递数据
        if (res.data.data != null) {
          that.setData({
            currentPage: currentPage + 1,
            totalElements: res.data.data.totalElements
          });
        }
       
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  getFeeling: function (e) {
    var that = this;
    that.setData({
      color: "#19b5ee"
    })
    wx.showLoading({
      title: '玩命打开中',
    })
    wx.downloadFile({
      url: app.globalData.bookUrl + encodeURIComponent(e.target.id).replace(/\%2F/g, "/"),
      success(res) {
        wx.hideLoading();
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath,
          success(res) {
            console.log('打开文档成功')
          },
          fail: function (error) {
            // fail
            wx.showToast({
              title: '打开失败',
              duration: 1000,
              image: '../../../images/icon/fail.png',
            })
            console.log("打开文档失败")
          }
        })
      }
    })
 

  },
  showEvaluate: function (evaluate, currentPage) {
    var that = this;
    var evaluates = that.data.evaluateList;  

    if (evaluate != null) {
      for (var idx in evaluate.content) {
        var content = evaluate.content[idx];
        var bookScore = content.bookScore;
        var evaluateContent = content.evaluateContent;
        var readersName = content.readersName;
        var headImg = content.headImg;
        var evaluateId = content.evaluateId;
        var praise = content.praise;
        var ispraise = content.ispraise;
        var evaluateFeeling = content.evaluateFeeling;
        var createTime = time.formatTime(content.createTime, 'Y-M-D h:m:s');
        var temp = {
          illume: bookScore,
          illume_no: 5 - bookScore,
          readersName: readersName,
          headImg: headImg,
          createTime: createTime,
          evaluateId: evaluateId,
          praise: praise,
          ispraise: ispraise,
          evaluateFeeling: evaluateFeeling,
          evaluateContent: evaluateContent,
        }
        evaluates.push(temp);
      }
      that.setData({
        evaluateList: evaluates
      });
      wx.hideLoading({
        title: '玩命加载中',
      })
    } else {
      wx.showToast({
        title: "没有更多了",
        icon: "success",
        duration: 1500
      })
    }
    
    
  },
  // 更改点赞状态
  onCollectionTap: function (event) {
    // 获取当前点击下标
    var that = this;
    var openId = wx.getStorageSync('openid');
    var evaluateId = event.currentTarget.dataset.id;
    var collected = event.currentTarget.dataset.index;
    var evaluate = this.data.evaluateList;
    if (collected == 0) { //如果是没点赞+1
      collected = 1;
      for (var idx in evaluate) {
        if (evaluate[idx].evaluateId == evaluateId) {
          evaluate[idx].ispraise = 1;
          evaluate[idx].praise += 1;
        }
      }
      this.setData({
        evaluateList: evaluate
      })
    } else {
      collected = 0;
      for (var idx in evaluate) {
        if (evaluate[idx].evaluateId == evaluateId) {
          evaluate[idx].ispraise = 0;
          evaluate[idx].praise -= 1;
        }
      }
      this.setData({
        evaluateList: evaluate
      })
    }
    // that.selectPraise(evaluateId).then(function(isParise){
    //    collected=isParise
    // })
    wx.request({
      url: that.data.praiseURL,
      data: {
        evaluateId: evaluateId,
        openid: openId,
        praise: collected,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        // that.setData({
        //   currentPage:  1,
        // });
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
 

  },
  clickNum: function (e) {
    var thisnum = e.target.dataset.num
    if (thisnum == 1){
      this.setData({
        _num: 0
      })
    }else{
      this.setData({
        _num: 1
      })
    }
  }, 

})